package com.haoxi.gateway.cmpp30msg;

/**
 * Title:CMPP3.0,2004.09
 */

import java.io.IOException;
import java.io.OutputStream;


/**
 * ʵ��CMPP3.0Э�� CMPPDeliverRep
 */
public class CMPPDeliverRep {

    private byte outbytes[];

    /**
     * ����һ��CMPPDeliverRep��Ϣ��
     *
     * @param Msg_Id        �μ�CMPP3.0
     * @param Tls_available �˲�������Ч
     * @param result        ����ֵ
     * @param sequence      Sequence
     */
    public CMPPDeliverRep ( int Sequence, byte[] Msg_Id, int result ) throws CMPP30Exception {
        if ( Msg_Id.length != 8 ) {
            throw new CMPP30Exception ( "CMPPDeliverRep Msg_Id is invalid!" );
        }
        byte[] bodybytes = new byte[12];
        System.arraycopy(Msg_Id,0,bodybytes,0,8);
        //CMPPCommon.BytesCopy ( Msg_Id, bodybytes, 0, 7, 0 );
        //bodybytes[11] = (byte)result;
        //Common.BytesCopy((byte)result,bodybytes,0,3,8);
        CMPPMsgHead head = new CMPPMsgHead ( CMPPCommon.MSG_HEAD_LENGTH + CMPPCommon.MSG_DELIVER_REP_BODY_LENGTH, CMPPCommon.CMPP_DELIVER_REP, Sequence );
        byte[] msgHead = head.getMsgHead ();
        outbytes = new byte[msgHead.length + bodybytes.length];
        System.arraycopy(msgHead,0,outbytes,0,msgHead.length);
        //CMPPCommon.BytesCopy ( msgHead, outbytes, 0, msgHead.length - 1, 0 );
        System.arraycopy(bodybytes,0,outbytes,msgHead.length,bodybytes.length);
        //CMPPCommon.BytesCopy ( bodybytes, outbytes, 0, bodybytes.length - 1, msgHead.length );
    }

    /**
     * ��CMPPDeliverRep���͵��ƶ����?
     *
     * @param out �����?
     * @throws ������IOΥ��
     */
    public void write ( OutputStream out ) throws IOException {
        out.write ( outbytes );
        out.flush ();
    }

    /**
     * ���ֽ��������ʽ����CMPP_DELIVER_REP��
     *
     * @return �ֽ�������ʽ��CMPP_DELIVER_REP��
     */
    public byte[] getBytes () {
        return outbytes;
    }
}
